//
//  SearchViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SearchViewController.h"
#import "CommonWebServices.h"
#import "DataList.h"
#import "Cmspubnews.h"
#import "SearchResultViewController.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [searchBar release];
    [requestArray release];
    [titleList release];
    [super dealloc];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [requestArray makeObjectsPerformSelector:@selector(cancelRequest)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = LS(@"搜索");
    requestArray = [[NSMutableArray alloc]init];
    
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.tintColor = TOOLBAR_TINTCOLOR;
    searchBar.delegate = self;
    searchBar.placeholder = LS(@"请输入关键字…");
    //[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
	[self.view addSubview:searchBar];
    
    titleList = [[NSMutableArray alloc]init];
    [self loadHotTitle];
}


- (void)loadHotTitle
{
    CommonWebServices *webServ = [[CommonWebServices alloc] init];
    webServ.tag = 1001;
    webServ.delegate = self;
    [requestArray addObject:webServ];
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setValue:@"10" forKey:@"Num"];
    [webServ startRequestWithMode:@"Default" Action:@"GetAllCmspubnews" Parma:parma Class:@"DataList" RootXML:@"GetAllCmspubnewsResult" resultIsArray:YES];
    [webServ release];
}

- (void)onCommonWebServicesWithCustomClass:(id)object tag:(NSInteger)tag{
    if (tag == 1001) {
        NSArray *list = ((DataList *)object).List;
        
        if ([list count]>0) {
            [titleList addObjectsFromArray:list];
            [self drawHotTitie];
        }
    }
    else if (tag == 1002) {
        //
    }
}

- (void)onCommonWebServicesFaild
{
    UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"加载失败")  
                                                        message:LS(@"请检查网络连接和设置")
                                                       delegate:self 	
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
}

- (void)drawHotTitie
{
    for (int i=0; i<[titleList count]; i++) {
        Cmspubnews *cmspubnews = [titleList objectAtIndex:i];
        
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setTitle:cmspubnews.News_title forState:UIControlStateNormal];
        titleBtn.frame = CGRectMake(10+150*(i%2), 44+ceil(i/2)*30, 150, 30);
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        titleBtn.tag = 1000+i;
        [titleBtn addTarget:self action:@selector(searchResult:) forControlEvents:UIControlEventTouchUpInside];
        [titleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [titleBtn setTitleColor:TOOLBAR_TINTCOLOR forState:UIControlStateHighlighted];
        [self.view addSubview:titleBtn];
    }
}

- (void)searchResult:(UIButton *)searchBtn
{
    Cmspubnews *cmspubnews = [titleList objectAtIndex:searchBtn.tag-1000];
    SearchResultViewController *searchResultViewController = [[SearchResultViewController alloc]init];
    searchResultViewController.searchKey = cmspubnews.News_title;
    [self.navigationController pushViewController:searchResultViewController animated:YES];
    [searchResultViewController release];
}

#pragma mark UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar{
	[searchBar resignFirstResponder];
	[searchBar setShowsCancelButton:NO animated:YES];
	
    if ([searchBar.text length]) {
        SearchResultViewController *searchResultViewController = [[SearchResultViewController alloc]init];
        searchResultViewController.searchKey = searchBar.text;
        [self.navigationController pushViewController:searchResultViewController animated:YES];
        [searchResultViewController release];
    }
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)_searchBar{
	[searchBar setShowsCancelButton:YES animated:YES];
	UIButton *cancelBt = (UIButton *)[searchBar.subviews objectAtIndex:2];
	[cancelBt setTitle:@"取消" forState:UIControlStateNormal];
	cancelBt.frame = CGRectMake(cancelBt.frame.origin.x, 
								cancelBt.frame.origin.y, 
								100, 
								cancelBt.frame.size.height);
	
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)_searchBar{
	[searchBar resignFirstResponder];
	[searchBar setShowsCancelButton:NO animated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
